// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Data;

namespace SnapObjects.Data
{
    internal class DataTableJsonExporter : IDataExporter
    {
        private readonly DataFormat _dataFormat = DataFormat.Json;
        private readonly IDataTableExporter _exporter;
        private readonly IDataTableExporter _plainExporter;

        public DataTableJsonExporter(DataTable dataTable)
        {
            _plainExporter = new DataTableExporter(dataTable);
        }

        public DataTableJsonExporter(IDataTableExporter exporter, DataTable dataTable)
            : this(dataTable)
        {
            _exporter = exporter;
        }

        public string Export(bool changedOnly)
        {
            if (_exporter != null)
            {
                return _exporter.Export(changedOnly, _dataFormat);
            }

            return this.ExportPlain(changedOnly);
        }

        public string Export()
        {
            return this.Export(-1);
        }

        public string Export(int startRow)
        {
            return this.Export(startRow, -1);
        }

        public string Export(int startRow, int endRow)
        {
            return this.Export(startRow, endRow, -1);
        }

        public string Export(int startRow, int endRow, int startColumn)
        {
            return this.Export(startRow, endRow, startColumn, -1);
        }

        public string Export(
            int startRow, int endRow, int startColumn, int endColumn)
        {
            var setting = new DataFormatSetting
            {
                StartRow = startRow,
                EndRow = endRow,
                StartProperty = startColumn,
                EndProperty = endColumn
            };

            if (_exporter != null)
            {
                return _exporter.Export(false, _dataFormat, setting);
            }

            return _plainExporter.Export(false, _dataFormat, setting);
        }

        public string ExportPlain(bool changedOnly)
        {
            return _plainExporter.Export(changedOnly, _dataFormat);
        }

        public string ExportPlain()
        {
            return this.ExportPlain(-1);
        }

        public string ExportPlain(int startRow)
        {
            return this.ExportPlain(startRow, -1);
        }

        public string ExportPlain(int startRow, int endRow)
        {
            return this.ExportPlain(startRow, endRow, -1);
        }

        public string ExportPlain(int startRow, int endRow, int startColumn)
        {
            return this.ExportPlain(startRow, endRow, startColumn, -1);
        }

        public string ExportPlain(
            int startRow, int endRow, int startColumn, int endColumn)
        {
            var setting = new DataFormatSetting
            {
                StartRow = startRow,
                EndRow = endRow,
                StartProperty = startColumn,
                EndProperty = endColumn
            };

            return _plainExporter.Export(false, _dataFormat, setting);
        }
    }
}
